package com.jxlc.web.controller.system;

import com.jxlc.common.annotation.Log;
import com.jxlc.common.core.controller.BaseController;
import com.jxlc.common.core.domain.AjaxResult;
import com.jxlc.common.enums.BusinessType;
import com.jxlc.web.api.system.domain.dto.FileDto;
import com.jxlc.web.api.system.service.SysFileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传
 * @author jxlc
 */
@RestController
@RequestMapping("/system/file")
public class SysFileController extends BaseController {
    
    @Resource
    private SysFileService sysFileService;

    /**
     * 新增文件
     */
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws Exception {
        return success(sysFileService.insert(file));
    }

    /**
     * 批量新增文件
     */
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping("/upload/batch")
    public AjaxResult upload(@RequestParam(value = "files") List<MultipartFile> files) throws Exception {
        if (files == null) {
            return error("文件不能为空");
        }
        List<FileDto> list = new ArrayList<>();
        for (MultipartFile multipartFile : files) {
            list.add(sysFileService.insert(multipartFile));
        }
        return success(list);
    }

    /**
     * 一键下载附件
     */
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/zip")
    public AjaxResult downloadSelectDocument(String fileIds, String fileName) {
        return success((Object) sysFileService.downloadZip(fileIds, fileName));
    }

}
